package com.sdx.console.wx;

import com.alibaba.fastjson.JSONObject;
import com.sdx.console.activity.web.ActivityActionPointController;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @ClassName: SMSController
 * @Description:
 * @author: kangbin
 * @date: 2019/6/25 15:08
 */
@RequestMapping("/wx/")
@Controller
public class MessageController {

    private static final Logger log = LoggerFactory.getLogger(MessageController.class);


    public void pushTemplateMsg(String openId,String formid,String keyword1,String keyword2,String keyword3){
        try {
            String page = "pages/index/index";
            //
//            String keyword1 = "志愿专家对2019年招生计划变化的直播正在进行中";
//            //
//            String keyword2 = "李文君老师";
//            //
//            String keyword3 = "资深升学规划专家；ASTD国际人才规划师；新浪、搜狐特邀请专家；人社部职业规划师";
            //
            String keyword4 = "";
            // 备注
            String keyword5 = "";

            //获取access_token  小七
            String access_token = WxApi.getAccessToken("wx3b064b7231ee014e", "900bdd50444a8b54a8cfc4c78bc63451");
            //推送微信接口
            JSONObject jsonObject = WxApi.wxPushMessage(access_token,openId,formid,"JUJTpV2b2POLn2ae2L7ja5bhljG0-v0eUbAk46c3bGo",page,keyword1,keyword2,keyword3,null,null);


            if (jsonObject!=null&&"ok".equals(jsonObject.getString("errmsg"))) {
                log.info("推送成功！openid="+openId+"   formid="+formid);
            }else{
                log.info("推送失败！openid="+openId+"   formid="+formid);
                throw new Exception("推送失败！openid="+openId+"   formid="+formid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/sms")
    public  void sms(HttpServletRequest request,String keyword1,String keyword2,String keyword3) throws IOException {
        log.info("推送标题！keyword1="+keyword1+"   keyword2="+keyword2+"   keyword3="+keyword3);
        if(StringUtils.isBlank(keyword1)||StringUtils.isBlank(keyword2)||StringUtils.isBlank(keyword3)){
            return;
        }
        String excelTemplate = "excelTemplates/order/formid.csv";
        InputStreamReader in =new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(excelTemplate), "GBK");
        BufferedReader reader = new BufferedReader(in);
        reader.readLine();
        String line = null;
        String openid,formid,flag;
//        pushTemplateMsg("ocb7y5NRn0GYg1u9_-6ipJgSA__o","ad97801c40294198949b60bbd2602e65",keyword1,keyword2,keyword3);
        while((line=reader.readLine())!=null){
            String item[] = line.split(",");//CSV格式文件为逗号分隔符文件，这里根据逗号切分
            openid = item[0];
            formid =item[1];
            flag =item[2];
            if ("1".equals(flag)) {
                continue;
            }
            pushTemplateMsg(openid,formid,keyword1,keyword2,keyword3);
        }
    }

}
